﻿Imports System.IO
Imports System.Xml
Public Class SyntaxHighlighter
    'TODO:
    '1. Code XML parser to get keywords to hightlight
    '2. Code main highlighting and break into groups
    '3. Incorporate into dev tools for testing
    Dim XMLR As XmlTextReader
    Public Function Retrieve_KeyWords(ByVal Lang As String, ByVal XMLR As XmlReader, ByVal KeyName As String)
        Dim XMLE As String
        While Not XMLR.GetAttribute("name") = KeyName
            XMLE = GetElement(XMLR, "Keywords")
        End While
        If XMLE = "Keywords" Then
            If XMLR.GetAttribute("name") = KeyName Then
                Dim name As String
                Dim style As String
                name = XMLR.GetAttribute("name")
                style = XMLR.GetAttribute("style")
                Dim words As New List(Of String)
                words.AddRange(XMLR.ReadContentAsString.Split(" "))
                Return words
            End If
        End If
    End Function
    Public Function GetElement(ByVal XMLR As XmlReader, ByVal ElName As String)
        While XMLR.IsStartElement
            Dim XMLE As String
            XMLR.Read()
            If XMLR.ReadElementString = ElName Then
                XMLE = XMLR.ReadElementString
                Return XMLE
                Exit While
            End If
        End While
    End Function
    Public Function RetrieveKeywordElements(ByVal XMLR As XmlReader, ByVal Element As String)
        Dim Elements As New List(Of String)
        While Not XMLR.EOF
            XMLR.Read()
            If XMLR.IsStartElement Then
                If XMLR.Name = "Language" Then
                    While Not XMLR.EOF
                        XMLR.ReadToNextSibling(Element)
                        If XMLR.Name = Element Then
                            Elements.AddRange(XMLR.ReadElementContentAsString.Split(" "))
                        End If
                    End While
                End If
            End If
        End While
        Return Elements
    End Function
    Public Function GetLanguage(ByVal DocType As String, ByVal XMLR As XmlReader)
        Dim Lang As String
        Dim ExtL As New List(Of String)
        Dim XMLE As String
        While Not XMLE
            XMLE = GetElement(XMLR, "Language")
            If XMLE = "Scavenger" Then
                Continue While
            End If
            If XMLE = "Language" Then
                Lang = XMLR.GetAttribute("name")
                ExtL.AddRange(XMLR.GetAttribute("ext").Split(" "))
                For Each extr As String In ExtL
                    If extr = DocType Then
                        Lang = XMLR.GetAttribute("name")
                        Return Lang
                        Exit While
                    Else
                        Continue While
                    End If
                Next
            End If
        End While
    End Function
    Public Function GetColor(ByVal Style As String, ByVal XMLR As XmlReader)
        Dim color As Color
        While Not XMLR.GetAttribute("name") = Style
            GetElement(XMLR, "WordStyle")
        End While
        If XMLR.GetAttribute("name") = Style Then
            color = Drawing.ColorTranslator.FromHtml(XMLR.GetAttribute("color"))
            Return color
        End If
    End Function
    Public Function GetDefines(ByVal XMLR As XmlReader, ByVal Lang As String)
        Dim defines As New List(Of String)
        While Not XMLR.EOF
            XMLR.Read()
            If XMLR.IsStartElement Then
                If XMLR.GetAttribute("name") = "defines" Then
                    defines.AddRange(XMLR.GetAttribute("name").Split(" "))
                End If
            End If
        End While
        Return defines
    End Function
    Public Sub SourceHightlight()
        XMLR = New XmlTextReader(CurDir() + "/plugins/SyntaxHighlighter.xml")
        Dim KeyWordEls As List(Of String)
        Dim lang As String
        lang = GetLanguage("html", XMLR)
        Dim Defines As List(Of String)
        Defines = GetDefines(XMLR, lang)
        KeyWordEls = RetrieveKeywordElements(XMLR, "Keywords")
        For Each element As String In KeyWordEls
            If element = "defines" Then
                Continue For
            End If
        Next
    End Sub
End Class
